// pages/article/articles/articles.js
let common = require('../../../utils/common.js');
Page({
  data: {
    articles: [],
    loadHidden: true,
    hasMore: false,
    tipNone: true,
  },
  skip: 0,
  take: 10,
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      loadHidden: false
    });
    this.reqArticles();
  },
  reqArticles: function () {
    let that = this;
    common.asynPost(common.API.article.articles, {
      skip: that.skip,
      take: that.take
    }, function (data) {
      that.setData({
        loadHidden: true
      });
      if (data['articles'] && data['articles'].length) {
        that.setData({
          articles: data['articles'],
          tipNone: true,
        });
      } else {
        that.skip -= that.take;
        that.setData({
          hasMore: true,
          tipNone: false,
        });
      }
    }, function () {
      that.setData({
        loadHidden: true
      });
    });
  },
  onReachBottom: function () {
    var that = this;
    that.setData({
      tipNone: false,
      hasMore: false
    });
    that.skip += that.take;
    that.reqArticles();
  },
  handleArticle:function(e){
    let that = this, id = e.currentTarget.dataset.id;
    if(id > 0){
      wx.navigateTo({
        url: '../detail/detail?id=' + id
      })
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})